from crv.model.crvmodel import StatusCode

from pygraph.classes.Digraph import digraph

class SC:
    nSC=8
    (SC_UNINITIALIZED,
     SC_INITIALIZED,
     SC_STARTING,
     SC_RUNNING,
     SC_STOPPING,
     SC_DEGRADED,
     SC_FIXING,
     SC_EXCEPTION)=range(nSC)
    SC_STARTED=SC_RUNNING
    SC_STOPPED=SC_INITIALIZED
    SC_UPGRADING=SC_DEGRADED
    canTarget=[SC_INITIALIZED,SC_RUNNING, SC_EXCEPTION]
    
SCname={}
for i in SC.__dict__.keys():
    if i.find('SC_')>=0:
        SCname[SC.__dict__[i]]=i
        

            
sg=digraph()
sg.add_nodes(SCname.keys())
sg.add_edge(SC.SC_UNINITIALIZED, SC.SC_INITIALIZED)
sg.add_edge(SC.SC_INITIALIZED,   SC.SC_STARTING)
sg.add_edge(SC.SC_STARTING,      SC.SC_STARTED)
sg.add_edge(SC.SC_STARTING,      SC.SC_EXCEPTION)
sg.add_edge(SC.SC_STARTED,       SC.SC_STOPPING)
sg.add_edge(SC.SC_STARTED,       SC.SC_DEGRADED)
sg.add_edge(SC.SC_STARTED,       SC.SC_EXCEPTION)
sg.add_edge(SC.SC_DEGRADED,      SC.SC_STARTED)
sg.add_edge(SC.SC_DEGRADED,      SC.SC_STOPPING)
sg.add_edge(SC.SC_STOPPING,      SC.SC_DEGRADED)
sg.add_edge(SC.SC_STOPPING,      SC.SC_INITIALIZED)
sg.add_edge(SC.SC_STOPPING,      SC.SC_EXCEPTION)
sg.add_edge(SC.SC_EXCEPTION,     SC.SC_FIXING)
sg.add_edge(SC.SC_FIXING,        SC.SC_INITIALIZED)

sgname=digraph()
sgname.add_nodes([SCname[i] for i in SCname.keys()])
sgname.add_edge(SCname[SC.SC_UNINITIALIZED], SCname[SC.SC_INITIALIZED])
sgname.add_edge(SCname[SC.SC_INITIALIZED],   SCname[SC.SC_STARTING])
sgname.add_edge(SCname[SC.SC_STARTING],      SCname[SC.SC_STARTED])
sgname.add_edge(SCname[SC.SC_STARTING],      SCname[SC.SC_EXCEPTION])
sgname.add_edge(SCname[SC.SC_STARTED],       SCname[SC.SC_STOPPING])
sgname.add_edge(SCname[SC.SC_STARTED],       SCname[SC.SC_DEGRADED])
sgname.add_edge(SCname[SC.SC_STARTED],       SCname[SC.SC_EXCEPTION])
sgname.add_edge(SCname[SC.SC_DEGRADED],      SCname[SC.SC_STARTED])
sgname.add_edge(SCname[SC.SC_DEGRADED],      SCname[SC.SC_STOPPING])
sgname.add_edge(SCname[SC.SC_STOPPING],      SCname[SC.SC_DEGRADED])
sgname.add_edge(SCname[SC.SC_STOPPING],      SCname[SC.SC_INITIALIZED])
sgname.add_edge(SCname[SC.SC_STOPPING],      SCname[SC.SC_EXCEPTION])
sgname.add_edge(SCname[SC.SC_EXCEPTION],     SCname[SC.SC_FIXING])
sgname.add_edge(SCname[SC.SC_FIXING],        SCname[SC.SC_INITIALIZED])

from pygraph.algorithms.searching import breadth_first_search
from pygraph.algorithms.accessibility import *
from pygraph.algorithms.minmax import *

shortestPathname={}
for i in SCname.keys():
    (tr,l)=shortest_path(sgname,SCname[i])
    for j in SC.canTarget:
        print "finding shortest path from %s to %s"%(SCname[i],SCname[j])
        f=i
        t=j
        path=[]
        while f!=t:
            if SCname[t] in tr.keys():
                path.append(SCname[t])
                t=SC.__dict__[tr[SCname[t]]]
            else:
                #already at root of the tree
                t=f
                print "asdf"
                path.append(SCname[t])
        path=path+[SCname[t]]
        path.reverse()
        print path
        shortestPathname[(SCname[i],SCname[j])]=path
        
        
shortestPath={}
for i in SCname.keys():
    (tr,l)=shortest_path(sg,i)
    for j in SC.canTarget:
        print "finding shortest path from %s to %s"%(SCname[i],SCname[j])
        f=i
        t=j
        path=[]
        while f!=t:
            if t in tr.keys():
                path.append(t)
                t=tr[t]
            else:
                #already at root of the tree
                t=f
                print "asdf"
                path.append(t)
        path=path+[t]
        path.reverse()
        print path
        print " ".join(SCname[i] for i in path)
        shortestPath[(i,j)]=path
        
        
